// 导入创建切片的方法
import { createSlice } from "@reduxjs/toolkit";

// 创建一个切片
const userSlice = createSlice({
    name: 'user', // 定义切片名称(模块名称)
    initialState: {
        // 定义全局的初始状态
        // 该属性是用来判断当前侧边栏是否为展开状态
        loginState: localStorage.getItem('loginState') === 'true',
        token: localStorage.getItem('token') || '',
        adminname: localStorage.getItem('adminname') || "",
        role: localStorage.getItem('role') * 1 || 1,
        checkedkeys: JSON.parse(localStorage.getItem('checkedkeys')) || [],
    },
    reducers:{
        changeUser(state, actions){
            // 用户登录成功之后的数据记录
            state.loginState = actions.payload.loginState
            state.token = actions.payload.token
            state.adminname = actions.payload.adminname
            state.role = actions.payload.role
            state.checkedkeys = actions.payload.checkedkeys
        }
    }
})
// 导出修改方法
export const { changeUser } = userSlice.actions
// 导出 reducer
export default userSlice.reducer